package com.bdqn.t320.security.repository;

import com.bdqn.t320.security.entity.User;
import com.bdqn.t320.security.vo.UserInfo;
import com.bdqn.t320.security.vo.UserRight;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface UserRepository  extends JpaRepository<User,Long>,JpaSpecificationExecutor<User> {

    public List<User> findByUsrName(String usrName);

    public User findByUsrNameAndUsrPassword(String usrName,String usrPassword);

//    @Query(value="select u from User u where u.usrRoleId=?1",nativeQuery = false)
//    public List<User> findByaaRoleId(Long roleId);
//
//    @Query(value = "select * from sys_user where usr_role_id=?1",nativeQuery = true)
//    public List<User> findByRoleId(Long roleId);
//
//    @Query("select u from User u where u.usrRoleId=?1")
//    public Page<User> findPageByUsrRoleId(Long roleId, Pageable pageable);

   // public Page<User> findPageByMap(Map param, Pageable pageable);

//    @Query("select u.usrId as usrId, u.usrName as usrName, u.usrPassword as usrPassword, " +
//            "u.usrRoleId as usrRoleId, u.usrFlag as usrFlag, r.roleName as roleName " +
//            "from User u, Role r where u.usrRoleId=r.roleId and u.usrId=?1")
//    public UserInfo getUserInfo(Long usrId);

    @Query(value="SELECT u.usr_id as usrId, u.usr_name as usrName, u.usr_password as usrPassword," +
            "u.usr_role_id as usrRoleId,r.role_name as roleName FROM sys_user u,sys_role r " +
            "where u.usr_role_id=r.role_id and u.usr_name=?1",nativeQuery = true)
    public UserInfo getUserInfo(String usrName);

    @Query(value="SELECT r.right_code as rightCode,r.right_parent_code as rightParentCode,r.right_type as rightType,\n" +
            "r.right_text as rightText,r.right_url as rightUrl,s.rf_id as rfId,s.rf_role_id as rfRoleId FROM `sys_right` r,\n" +
            "(select * from sys_role_right where rf_role_id = (select role_id from sys_role " +
            "where role_id = (select usr_role_id from sys_user where usr_name = ?1))) s\n" +
            "where r.right_code = s.rf_right_code and r.right_type <> 'Folder';",nativeQuery = true)
    public List<UserRight> getUserRightByUserName(String usrName);
}
